package com.boss.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import com.boss.example.entity.dto.MenusDTO;
import com.boss.example.entity.dto.RolesDTO;
import com.boss.example.entity.vo.MenusVO;
import com.boss.example.entity.vo.RolesVO;
import com.boss.example.entity.vo.CommonResult;
import com.boss.example.service.MenusService;

/**
 * @author Hu_liantian
 * @date 2020/8/6
 */
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenusController {

    @Resource
    private MenusService menuService;

    @PostMapping("/insert")
    public CommonResult insertMenu(@RequestBody MenusVO menuVO){
        MenusDTO menusDTO = new MenusDTO();
        BeanUtils.copyProperties(menuVO, menusDTO);
        log.info(menusDTO.toString());
        int result = menuService.insertRolesMenu(menusDTO);
        if (result > 0) {
            return new CommonResult(200, "插入成功", result);
        } else {
            return new CommonResult(400, "插入失败", null);
        }
    }

    @PostMapping("/update")
    public CommonResult updateMenu(@RequestBody MenusVO menuVO) {
        MenusDTO menusDTO = new MenusDTO();
        BeanUtils.copyProperties(menuVO, menusDTO);
        int result = menuService.insertRolesMenu(menusDTO);
        if (result > 0) {
            return new CommonResult(200, "更改成功", result);
        } else {
            return new CommonResult(400, "更改失败", null);
        }
    }

    @PostMapping("/delete")
    public CommonResult deleteMenu(@RequestBody MenusVO menuVO){
        MenusDTO menusDTO = new MenusDTO();
        BeanUtils.copyProperties(menuVO, menusDTO);
        if(menusDTO != null){
            Integer id = menusDTO.getMenuId();
            int result = menuService.deleteRolesMenu(id);
            if (result > 0) {
                return new CommonResult(200, "删除成功,id:", id);
            }
        }
        return new CommonResult(400, "删除失败", null);
    }

    @PostMapping("/getMenu")
    public CommonResult getMenuByRoleId(@RequestBody RolesVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        Integer roleId = rolesDTO.getRoleId();
        if(roleId != null){
            List<MenusDTO> menusDTOList = menuService.getMenuByRoleId(roleId);
            if(menusDTOList != null){
                return new CommonResult(200, "查找成功", menusDTOList);
            }
        }
        return new CommonResult(400, "查找失败",null );
    }

}
